import UIKit

//代理 ------ 1.协议

protocol ZSVistorViewDelegate: NSObjectProtocol
{
    //默认是必须实现的代码
    func vistorViewLoginClick()
}


///访客视图
class ZSVistorView: UIView {
    
//代理 ------ 2.定义属性
    weak var delegate: ZSVistorViewDelegate?
    
    
    //swift默认所有的View都能通过xib/storyboard 来加载
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        //super是要在设置普通的存储型属性下面
        super.init(frame: frame)
        
        //设置背景颜色
        backgroundColor = UIColor(white: 237 / 255.0, alpha: 1)
        
        prepareUI()
    }
    
    //MARK: - 登陆按钮点击事件
    //UIView 中无法处理按钮点击事件，所以必须交给控制器处理，这个时候就需要用到代理或者闭包
    func loginClick()
    {
//代理 ------ 3.通知代理
        delegate?.vistorViewLoginClick()
    }
    
    //MARK - 设置访客视图的内容
    func setUpVistorView(imageName: String,message: String)
    {
        //图片，替换转轮的图片
        iconView.image=UIImage(named: imageName)
        //文字
        messageLabel.text=message
        //自适应大小
        messageLabel.sizeToFit()
        //隐藏小房子
        homeView.hidden=true
//        coverView.hidden=true
        //父控件把coverView移动到最后面
        sendSubviewToBack(coverView)
        
    }
    
    //打印验证动画是否被移除
//    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
//        let a = iconView.layer.animationForKey("rotation")
//        print("动画 a:\(a)")
//    }
    
    //MARK: - 转轮旋转动画
    func rotationAnimation()
    {
        //核心动画
        let rotation = CABasicAnimation(keyPath: "transform.rotation")
        //设置动画参数
        rotation.toValue = 2 * M_PI
        rotation.duration=20
        rotation.repeatCount=MAXFLOAT
        //当不可见的时候，系统会认为动画完成，在完成的时候不要移除
        rotation.removedOnCompletion=false
        //开始核心动画
        iconView.layer.addAnimation(rotation, forKey: "rotation")
    }
    //MARK - 核心动画的暂停和开始
    /// 暂停旋转
    func pauseAnimation() {
        // 记录暂停时间
        let pauseTime = iconView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil)
        
        // 设置动画速度为0
        iconView.layer.speed = 0
        
        // 设置动画偏移时间
        iconView.layer.timeOffset = pauseTime
    }
    
    /// 恢复旋转
    func resumeAnimation() {
        // 获取暂停时间
        let pauseTime = iconView.layer.timeOffset
        
        // 设置动画速度为1
        iconView.layer.speed = 1
        
        iconView.layer.timeOffset = 0
        
        iconView.layer.beginTime = 0
        
        let timeSincePause = iconView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil) - pauseTime
        
        iconView.layer.beginTime = timeSincePause
    }
    

    ///准备UI
    private func prepareUI()
    {
        //1.添加子控件
        addSubview(iconView)
        //在这里添加遮罩视图
        addSubview(coverView)
        addSubview(homeView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        //2.添加约束 autolayout
        //注意：要设置约束的view，只有添加到父控件之后才能设置自动布局
        //关闭autoresizing,不让它受autolayout的约束
        iconView.translatesAutoresizingMaskIntoConstraints=false
        coverView.translatesAutoresizingMaskIntoConstraints=false
        homeView.translatesAutoresizingMaskIntoConstraints=false
        messageLabel.translatesAutoresizingMaskIntoConstraints=false
        registerButton.translatesAutoresizingMaskIntoConstraints=false
        loginButton.translatesAutoresizingMaskIntoConstraints=false
        
        
        
        //设置约束
        //背景转盘
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
        
        //小房子
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))

        //消息
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 226))
        
        //注册按钮
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 90))
        
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        //登陆按钮
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 90))
        
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        //遮罩视图约束
        //左
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
//        //上
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
//        //右
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
//        //下
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        
    }
    
    
    
    //MARK: - 懒加载
    ///加载转轮
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))

    ///小房子
    private lazy var homeView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    ///消息Label
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.redColor()
        //设置label文本内容
        label.text = "关注一些人，看看有什么惊喜"
        label.numberOfLines = 0
        //根据文本来适应大小
        label.sizeToFit()
        return label
    }()
    
    
    //注册按钮
    private lazy var registerButton: UIButton = {
        
        let button = UIButton()//创建按钮
        //设置按钮的图片
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        //设置按钮文字
        button.setTitle("注册", forState: UIControlState.Normal)
        //设置文本的颜色
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        //设置文本的大小
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        //根据内容适应大小
        button.sizeToFit()
        
        return button
    }()
    
    //登陆按钮
    private lazy var loginButton: UIButton = {
        
        let button = UIButton()//创建按钮
        //设置按钮的图片
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        //设置按钮文字
        button.setTitle("登陆", forState: UIControlState.Normal)
        //设置文本的颜色
        button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        //设置文本的大小
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        //根据内容适应大小
        button.sizeToFit()
        //添加点击事件
        button.addTarget(self, action: "loginClick", forControlEvents: UIControlEvents.TouchUpInside)
        return button
        }()
    
    // 遮罩视图
    private lazy var coverView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
}
